﻿CREATE VIEW [dbo].[ASMS_V_MaintenanceBillServiceItem]
	AS 
SELECT MBSI.*,
	WB.Id as WorkingBillId,
	WB.FinishedTime as ActualFinishedTime,
	WB.TechnicianId as ActualTechnicianId,
	WB.[Status] as ActualStatus,
	S0.Name as TechnicianName,
	S1.Name as ActualTechnicianName,
	SI.Name as ServiceItemName,
	S2.Name as SalesManagerName
FROM [ASMS_MaintenanceBillServiceItem] as MBSI
	left join ASMS_WorkingBill as WB on WB.MaintenanceBillServiceItemId = MBSI.Id and WB.[Status] <> 2
	left join ASMS_Staff as S0 on S0.Id = MBSI.TechnicianId
	left join ASMS_Staff as S1 on S1.Id = WB.TechnicianId
	left join ASMS_Staff as S2 on S2.Id = MBSI.SalesManagerId
	left join ASMS_ServiceItem as SI on SI.Id = MBSI.ServiceItemId
